﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.FrmKP
{
    /// <summary>
    /// 回写/修正税控号码-整单
    /// </summary>
    public partial class FrmReturn_Rd : Form
    {
        private WriteBackType _itype;
        private int _id;

        /// <summary>
        /// 回写/修正税控号码
        /// </summary>
        /// <param name="fpdm"></param>
        /// <param name="fphm"></param>

        public FrmReturn_Rd(int id, string fpdm, string fphm, WriteBackType itype, DateTime ddate, out string err)
        {
            try
            {
                InitializeComponent();
                this.Icon = YTSK.Properties.Resources.Fly;
                ddate = new DateTime(ddate.Year, ddate.Month, ddate.Day);
                this.dateTimePicker1.Value = ddate;
                _id = id;
                _itype = itype;
                SetFPHM(fpdm, fphm);
                setLabel();
                err = "";
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }

        }
        private void SetFPHM(string fpdm, string fphm)
        {
            var fpdmsave = ""; var fphmsave = "";
            if (_itype == WriteBackType.回写发票)
            {
                var url = string.Format("{0}/api/oms/SKSalebillVouch/GetNextVouchCode?token={1}",
                                ServerConfig.ServerAddress, ServerConfig.GetToken());
                var dic = new Dictionary<string, object>();
                dic.Add("fpdm", fpdm);
                dic.Add("fphm", fphm);
                dic.Add("IDRD", _id);
                //dic.Add("cBillType", _rd.cBillType);
                var s = Common.handleHttp.Post(dic, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { return; }
                fpdmsave = result.value["fpdm"].ObjToString();
                fphmsave = result.value["fphm"].ObjToString();
                if (result.resultdetail.ObjToString().Length > 0)
                {
                    MessageBox.Show(result.resultdetail);
                }
            }
            if (fpdm.ObjToString().Length > 0) { this.tbfpdm.Text = fpdm; }
            if (fphm.Length>0 & fpdmsave.ObjToString().Length > 0) { this.tbfpdm.Text = fpdmsave; }
            if (fpdm == fpdmsave & fpdm.ObjToString().Length > 0) { this.tbfpdm.ReadOnly = true; }
            else { this.tbfpdm.ReadOnly = false; }

            if (fphm.ObjToString().Length > 0) { this.tbfphm.Text = fphm; }
            if (fphmsave.ObjToString().Length > 0) { this.tbfphm.Text = fphmsave; }
            if (fphm == fphmsave & fphm.ObjToString().Length > 0) { this.tbfphm.ReadOnly = true; }
            else { this.tbfphm.ReadOnly = false; }

        }

        #region 设置label的信息
        private void setLabel()
        {
            switch (_itype)
            {
                case WriteBackType.回写发票:
                    this.Text = "发票回写";
                    this.lbtip.Text = "请确认发票打印是否正确？";
                    this.btOK.Text = "确认回写";
                    this.dateTimePicker1.Enabled = false;
                    break;
                case WriteBackType.手工回写:
                    this.Text = "手工回写";
                    this.lbtip.Text = "请确认发票号是否正确？";
                    this.btOK.Text = "手工回写";
                    this.dateTimePicker1.Enabled = true;
                    break;
                case WriteBackType.冲红字:
                    this.dateTimePicker1.Enabled = true;
                    this.Text = "发票冲红字--跨月";
                    this.lbtip.Text = "请输入红冲后的发票信息：";
                    this.btOK.Text = "确认红冲";
                    break;
                case WriteBackType.修正发票号码:
                    this.dateTimePicker1.Enabled = true;
                    this.Text = "发票号码修正";
                    this.lbtip.Text = "请输入修正后的发票信息：";
                    this.btOK.Text = "确认修正";
                    break;
            }
        }
        #endregion


        #region 回写发票
        private void btOK_Click(object sender, EventArgs e)
        {
            try
            {
                string url = "";
                switch (_itype)
                {
                    case WriteBackType.回写发票:
                    case WriteBackType.手工回写:
                        url = string.Format("{0}/api/oms/SKSalebillVouch/ReturnVouchAll?token={1}",
                            ServerConfig.ServerAddress, ServerConfig.GetToken());
                        break;
                    case WriteBackType.冲红字:
                        url = string.Format("{0}/api/oms/SKSalebillVouch/RedVouch?token={1}",
                         ServerConfig.ServerAddress, ServerConfig.GetToken());
                        break;
                    case WriteBackType.修正发票号码:
                        url = string.Format("{0}/api/oms/SKSalebillVouch/ModifyVouchCode?token={1}",
                            ServerConfig.ServerAddress, ServerConfig.GetToken());
                        break;
                }
                var pos = new Dictionary<string, object>();

                pos.Add("ID", _id);
                string fpdm = this.tbfpdm.Text.Trim();
                string fphm = this.tbfphm.Text.Trim();
                var err = CheckFP(fpdm, fphm);
                if (err.Length > 0) { MessageBox.Show(err); return; }
                pos.Add("fpdm", fpdm);
                pos.Add("fphm", fphm);
                var ddate = this.dateTimePicker1.Value.Date;
                pos.Add("dFPDate", ddate);

                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result != 1) { MessageBox.Show("回写发票号出现错误" + result.resultdetail); return; }
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 检查发票号码及发票代码的长度
        private string CheckFP(string fpdm, string fphm)
        {
            var err = "";
            //统一为8位
            if (fphm.Length != 8)
            {
                err += string.Format("发票号码的长度必须为8位，[{0}]的长度[{1}]位不正确", fphm, fphm.Length);
            }
            //电子发票12位，普通发票和专用发票10位
            if (fpdm.Length != 10 && fpdm.Length != 12)
            {
                err += string.Format("发票代码的长度必须为10或12位，[{0}]的长度[{1}]位不正确", fpdm, fpdm.Length);
            }
            return err;
        }
        #endregion


        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
